﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace Green.AgileMapper
{
    public class DataRowMappingRule : IMappingRule
    {
        public string FromPoperty
        { get; private set; }

        public string ToPoperty
        { get; private set; }

        public DataRowMappingRule(string fromPoperty, string toPoperty)
        {
            this.FromPoperty = fromPoperty;
            this.ToPoperty = toPoperty;
        }
        #region IMappingRule 成员
        /// <summary>
        /// 根据 To得到From
        /// </summary>
        /// <param name="from"></param>
        /// <param name="to"></param>
        public virtual void WarpFrom(object from, object to)
        {
            var row = from as DataRow;
            var rowTo = to as DataRow;
            object toValue = null;
            if (rowTo != null )
            {
                if (rowTo.Table.Columns.Contains(this.ToPoperty))
                {
                    toValue = rowTo[this.ToPoperty];
                }
            }
            else
            {
                toValue = Green.FastInvoker.PropertyAccessor.Get(to, this.ToPoperty);
            }


            if (row != null )
            {
                if (row.Table.Columns.Contains(this.FromPoperty))
                {
                    if (toValue == null)
                    {
                        row[this.FromPoperty] = DBNull.Value;
                    }
                    else
                    {
                        row[this.FromPoperty] = toValue;
                    }
                }
            }
            else 
            {
                if (toValue != DBNull.Value)
                {
                    Green.FastInvoker.PropertyAccessor.Set(from, this.FromPoperty, toValue);
                }
            }
        }
        /// <summary>
        /// 根据From得到To
        /// </summary>
        /// <param name="from"></param>
        /// <param name="to"></param>
        public virtual void WarpTo(object from, object to)
        {
            var row = from as DataRow;
            var rowTo = to as DataRow;
            object fromValue = null;
            if (row != null )
            {
                if (row.Table.Columns.Contains(this.FromPoperty))
                {
                    fromValue = row[this.FromPoperty];
                }
            }
            else
            {
                fromValue = Green.FastInvoker.PropertyAccessor.Get(from, this.FromPoperty);
            }

            if (rowTo != null)
            {
                if (rowTo.Table.Columns.Contains(this.ToPoperty))
                {
                    if (fromValue != null)
                    {
                        rowTo[this.ToPoperty] = fromValue;
                    }
                    else
                    {
                        rowTo[this.ToPoperty] = DBNull.Value;
                    }
                }
            }
            else
            {
                if (fromValue != DBNull.Value)
                {
                    Green.FastInvoker.PropertyAccessor.Set(to, this.ToPoperty, fromValue);
                }
            }
        }

        #endregion

        #region IXmlConfigLoad 成员

        public void LoadXmlConfig(Dictionary<string, object> propertys)
        {
            throw new NotImplementedException();
        }

        #endregion
    }
}
